package org.zfes.snowier.cloud.zbss.widget.act;

import java.io.File;
import java.util.Date;
import org.springframework.web.multipart.MultipartFile;
import org.zfes.snowier.core.util.UUIDUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZStrUtil;

public class UpLoadUitl {
	public static  Boolean isDocFile(String fileName) {
	        String [] img_type = new String[]{".pdf","xlsx","xls",".docx",".doc",".jpg",".JPG",".jpeg",".JPEG",".png",".PNG",".bmp",".BMP"};
	        if(fileName==null) {return false;}
	        fileName = fileName.toLowerCase();
	        for(String type : img_type) {
	            if(fileName.endsWith(type)) {return true;}
	        }
	        return false;
	    }
	public static  Boolean isImgFile(String fileName) {
        String [] img_type = new String[]{".jpg",".JPG",".jpeg",".JPEG",".png",".PNG",".bmp",".BMP"};
        if(fileName==null) {return false;}
        fileName = fileName.toLowerCase();
        for(String type : img_type) {
            if(fileName.endsWith(type)) {return true;}
        }
        return false;
    }


	public static String getFileType(String fileName) {
	        if(fileName!=null && fileName.indexOf(".")>=0) {
	            return fileName.substring(fileName.lastIndexOf("."), fileName.length());
	        }
	        return "";
	    }
	
	

	private static String getAbsolutepath(String imagePath) {	 //存储到跟jar包相同路径下 upload/biz/xx.png
		 String absolutePath=new StringBuilder().append(ZStrUtil.substringBeforeLast(new File(".").getAbsolutePath(), ".")).append(File.separator)
				 .append(BizConsts.fileRootPath).append(File.separator) 
				 .append(imagePath).toString();
		 return absolutePath;
	}
	
	private static String getSubPath(String fileBizPath) {
		 Date now =ZDateUtil.newDate();
		 StringBuilder imagePath = new StringBuilder()
				 		.append(BizConsts.docRootPath).append(File.separator)
				 		.append(fileBizPath).append(File.separator)
				 		.append(ZDateUtil.getYear(now)).append(File.separator)
						.append(ZDateUtil.getMonth(now)).append(File.separator)
						.append(ZDateUtil.getDay(now)).append(File.separator);
		 return imagePath.toString();
	}
	
	public static  String tranferFileImg(MultipartFile file,String fileBizPath) {
		String image=new String("");
		 try {
				 String fileName = file.getOriginalFilename();
				 ZAssert.hasText(fileName, "文档名称错误");
				 ZAssert.isTrue(UpLoadUitl.isImgFile(fileName), "不支持的文件格式");
				 //ZAssert.bigThanZero(2*1024 * 1024-file.getSize() , "图片大小不能超过2MB", "图片大小不能超过2MB");
				 //ZAssert.bigThanZero(800* 1024-file.getSize() , "图片大小不能超过200KB", "图片大小不能超过200KB");
				 
				 Date now =ZDateUtil.newDate();
				 
				 StringBuilder imagePath = new StringBuilder()
						 		.append(BizConsts.docRootPath).append(File.separator)
						 		.append(fileBizPath).append(File.separator)
						 		.append(ZDateUtil.getYear(now)).append(File.separator)
								.append(ZDateUtil.getMonth(now)).append(File.separator)
								.append(ZDateUtil.getDay(now)).append(File.separator);
				 image=imagePath.toString();
				 //存储到跟jar包相同路径下 upload/biz/xx.png
				 String absolutePath=new StringBuilder().append(ZStrUtil.substringBeforeLast(new File(".").getAbsolutePath(), ".")).append(File.separator)
						 .append(BizConsts.fileRootPath).append(File.separator) 
						 .append(imagePath).toString();
				 
				 if (!new File(absolutePath).exists()){  
		               File dir = new File(absolutePath);  
		               dir.mkdirs();  
		          } 
				  
				 String newFileName=UUIDUtil.base58Uuid()+ UpLoadUitl.getFileType(fileName);
				 image=image+newFileName;
				 String outFileFullName=absolutePath + File.separator + newFileName;
				 
				 File outFile = new File(outFileFullName);
				 file.transferTo(outFile);
				
				 
		 } catch (Exception e) {
			ZAlert.appRuntimeError(e.getMessage());
		 }
		 image=image.replace(File.separatorChar, '/');
		 return image;
	}
	public static  String tranferFile(MultipartFile file,String fileBizPath) {
		String image=new String("");
		 try {
				 String fileName = file.getOriginalFilename();
				 ZAssert.hasText(fileName, "文档名称错误");
				 ZAssert.isTrue(UpLoadUitl.isDocFile(fileName), "不支持的文件格式");
				 //ZAssert.bigThanZero(2*1024 * 1024-file.getSize() , "图片大小不能超过2MB", "图片大小不能超过2MB");
				 //ZAssert.bigThanZero(800* 1024-file.getSize() , "图片大小不能超过200KB", "图片大小不能超过200KB");
				 
				 Date now =ZDateUtil.newDate();
				 
				 StringBuilder imagePath = new StringBuilder()
						 		.append(BizConsts.docRootPath).append(File.separator)
						 		.append(fileBizPath).append(File.separator)
						 		.append(ZDateUtil.getYear(now)).append(File.separator)
								.append(ZDateUtil.getMonth(now)).append(File.separator)
								.append(ZDateUtil.getDay(now)).append(File.separator);
				 image=imagePath.toString();
				 //存储到跟jar包相同路径下 upload/biz/xx.png
				 String absolutePath=new StringBuilder().append(ZStrUtil.substringBeforeLast(new File(".").getAbsolutePath(), ".")).append(File.separator)
						 .append(BizConsts.fileRootPath).append(File.separator) 
						 .append(imagePath).toString();
				 
				 if (!new File(absolutePath).exists()){  
		               File dir = new File(absolutePath);  
		               dir.mkdirs();  
		          } 
				  
				 String newFileName=UUIDUtil.base58Uuid()+ UpLoadUitl.getFileType(fileName);
				 image=image+newFileName;
				 String outFileFullName=absolutePath + File.separator + newFileName;
				 
				 File outFile = new File(outFileFullName);
				 file.transferTo(outFile);
				
				 
		 } catch (Exception e) {
			ZAlert.appRuntimeError(e.getMessage());
		 }
		 image=image.replace(File.separatorChar, '/');
		 return image;
	}
}
